<template>
  <div>
    <!-- 预览用户 渲染数据  -->
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>用户列表</span>
        <el-button type="primary" size="mini" @click="addCateShowDialogBtnFn">添加用户</el-button>
         <!-- 分类数据的表格 -->
      <el-table :data="tableData" style="width: 100%" border stripe>
        <!-- type是table-column内置属性，你告诉他用index，就是单元格用索引值 -->
        <el-table-column type="index" label="序号" width="100"></el-table-column>
        <el-table-column prop="cate_name" label="账号"></el-table-column>
        <el-table-column prop="cate_alias" label="密码"></el-table-column>
        <el-table-column label="操作">
          <!-- scope对象：{row:行对象} -->
          <template v-slot="scope">
            <el-button type="primary" size="mini" @click="updateCateBtnFn(scope.row)">修改</el-button>
            <el-button type="danger" size="mini" @click="delCateFn(scope.row)">删除</el-button>
          </template>

        </el-table-column>
      </el-table>
      </div>

    </el-card>
    <!-- 添加用户 -->
    <el-dialog
      title="添加用户"
      :visible.sync="dialogVisible"
      width="30%"
      @close="dialogCloseFn"
      >
      <el-form :model="addForm" :rules="addRules" ref="addRef" label-width="80px">
        <el-form-item label="用户账号" prop="cate_name">
          <el-input v-model="addForm.cate_name" minlength="1" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="用户密码" prop="cate_alias">
          <el-input v-model="addForm.cate_alias" minlength="1" maxlength="15"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="confirmFn">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>

<script>
// import { getArtCateListAPI, addArtCateListAPI, updateArtCateAPI, deleteArtCateAPI } from '@/api/user'
export default {
  name: 'HotelSystemAdminUserM',

  data () {
    return {
      tableData: [{
        date: '2016-05-02',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1518 弄'
      }, {
        date: '2016-05-04',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1517 弄'
      }, {
        date: '2016-05-01',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1519 弄'
      }, {
        date: '2016-05-03',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1516 弄'
      }],
      cateList: [], // 文章分类列表
      dialogVisible: false, // 控制对话框的出现/隐藏
      addForm: { // 添加表单的数据对象
        cate_name: '',
        cate_alias: ''
      },
      addRules: { // 添加表单的验证规则对象
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      },
      isEdit: false, // true为编辑状态 false为新增状态
      editId: '' // 保存正在要编辑的数据id值
    }
  },

  mounted () {

  },

  methods: {
    async getArtCateFn () {
      const { data: res } = await (this.addForm)
      // console.log(res)
      this.cateList = res.data
    },
    // 点击确定，调用接口
    confirmFn () {
      // 调用前的兜底校验
      this.$refs.addRef.validate(async valid => {
        if (valid) {
          // 通过校验
          if (this.isEdit) {
            // 修改数据
            this.addForm.id = this.editId
            const { data: res } = await (this.addForm)
            if (res.code !== 0) return this.$message.error(res.message)
            // 更新成功
            this.$message.success(res.message)
          } else {
            // 新增数据
            const { data: res } = await (this.addForm)
            if (res.code !== 0) return this.$message.error(res.message)
            // 添加成功
            this.$message.success(res.message)
          }
          // 重新获取文章分类列表
          this.getArtCateFn()
          this.dialogVisible = false
        } else {
          return false
        }
      })
    },
    // 添加分类按钮点击事件，让对话框出现
    addCateShowDialogBtnFn () {
      this.dialogVisible = true
      this.isEdit = false // 变回新增状态
      this.editId = ''
    },
    // 弹窗关闭的回调
    dialogCloseFn () {
      this.$refs.addRef.resetFields()
    },
    // 修改分类按钮的点击事件 先做数据回显
    updateCateBtnFn (obj) {
      // obj的值就是id, cate_name, cate_alias
      console.log(obj)
      this.dialogVisible = true
      this.isEdit = true
      this.editId = obj.id
      this.$nextTick(() => {
        // 数据回显
        this.addForm.cate_name = obj.cate_name
        this.addForm.cate_alias = obj.cate_alias
      })
    },
    // 删除按钮的点击事件
    async delCateFn (obj) {
      console.log(obj)
      const { data: res } = await (obj.id)
      if (res.code !== 0) return this.$message.error(res.message)
      // 删除成功
      this.$message.success(res.message)
      // 重新获取
      this.getArtCateFn()
    }
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
